package com.snoob.springboot.workorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snoob.springboot.base.BaseException;
import com.snoob.springboot.util.StrUtil;
import com.snoob.springboot.workorder.entity.WorkOrderEnterpriseEntity;
import com.snoob.springboot.workorder.mapper.WorkOrderEnterpriseMapper;
import com.snoob.springboot.workorder.service.WorkOrderEnterpriseService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 企业工单之企业信息（企业、企业用户） 服务实现类
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2022-05-15
 */
@Service
public class WorkOrderEnterpriseServiceImpl extends ServiceImpl<WorkOrderEnterpriseMapper, WorkOrderEnterpriseEntity> implements WorkOrderEnterpriseService {

    /**
     * @描述 添加工单的子表信息（企业、企业用户）
     */
    @Override
    public Long insertWorkOrderEnterprise(WorkOrderEnterpriseEntity entity) {
        // 非空校验
        if (null == entity) {
            throw new BaseException("参数缺失");
        }
        boolean insertWorkOrderEnterpriseFlag = this.save(entity);
        return insertWorkOrderEnterpriseFlag ? entity.getId() : null;
    }

    /**
     * @描述 添加工单的子表信息（企业、企业用户）
     */
    @Override
    public WorkOrderEnterpriseEntity selectWorkOrderEnterprise(WorkOrderEnterpriseEntity entity) {
        // 非空校验
        if (null == entity) {
            throw new BaseException("参数缺失");
        }
        // 必填字段校验
        if (!StrUtil.isEffective(entity.getEnterpriseCode())) {
            throw new BaseException("企业编码缺失");
        }
        if (!StrUtil.isEffective(entity.getUserAccount())) {
            throw new BaseException("用户账号缺失");
        }
        // 查询条件
        QueryWrapper<WorkOrderEnterpriseEntity> workOrderEnterpriseEntityQueryWrapper = new QueryWrapper<>();
        workOrderEnterpriseEntityQueryWrapper.eq("enterprise_code", entity.getEnterpriseCode());
        workOrderEnterpriseEntityQueryWrapper.eq("user_account", entity.getUserAccount());
        entity = this.getOne(workOrderEnterpriseEntityQueryWrapper);
        return entity;
    }
}
